package com.csr.csrcheck.csrhtcontroller;

import com.csr.csrcheck.pojo.*;
import com.csr.csrcheck.service.impl.ProductServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping({"product"})
public class ProductServlet {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.csrhtcontroller.ProductServlet.class);

    @Resource
    private ProductServiceImpl productService;

    @RequestMapping({"addproduct"})
    public String addproduct(Model model) {
        List<Company> company = this.productService.selectCompany();
        List<Product_type> typelist = this.productService.selectProduct_type();
        List<Patent_type> patent = this.productService.selectPatent();
        List<Stage> stages = this.productService.selectStage();
        model.addAttribute("company", company);
        model.addAttribute("typelist", typelist);
        model.addAttribute("patent", patent);
        model.addAttribute("stages", stages);
        return "addproduct";
    }

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @RequestMapping(path = {"saveproduct"}, method = {RequestMethod.GET})
    public String addProduct(Product product, @RequestParam("term_of_validity") String date) throws ParseException {
        product.setTerm_of_validity(sdf.parse(date));
        int result = this.productService.addproduct(product);
        if (result == 1) {
            log.info("");
        } else {
            log.info("");
        }
        return "Product";
    }
}
